/* Universidad del Valle de Guatemala
 * Sara López 12050
 * Matías Vasquez 12186
 * Nombre: Stack.java
 * Sección:21
 */

import java.io.*;
import com.sun.java.swing.plaf.windows.WindowsFileChooserUI;
import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Matias
 */
public class Grafico extends javax.swing.JFrame {
    static JFrame t = new JFrame();
    static boolean bandera = false;
    /**
     * Creates new form Grafico
     */
    public Grafico() throws SingletonException {
        if (bandera) {
            throw new SingletonException("Sólo se permite una instancia de la Calculadora Postfix");
        } else {
            bandera = true;
            initComponents();
        }
    }

    private void terminar()
    {
        bandera = false; 
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        db = new javax.swing.JFileChooser();
        buttonGroup1 = new javax.swing.ButtonGroup();
        jComboBox1 = new javax.swing.JComboBox();
        jComboBox1.setVisible(false);
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jButton5 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Simplemente encadenada", "Doblemente encadenada", "Circular" }));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Seleccione el metodo para calcular el resultado");

        jLabel2.setText("Seleccione el archivo de texto con las operaciones:");

        jButton4.setText("...");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jRadioButton1.setText("ArrayList");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        jRadioButton2.setText("Vector");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        jRadioButton3.setText("List");
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });

        jButton5.setText("Calcular");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 62, Short.MAX_VALUE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(54, 54, 54))))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(106, 106, 106)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(21, 21, 21)
                                .addComponent(jButton5))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jRadioButton3)
                                .addGap(40, 40, 40)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jRadioButton2)
                            .addComponent(jRadioButton1)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(54, 54, 54)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jButton4))
                .addGap(18, 18, 18)
                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jRadioButton3)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton5)
                .addGap(44, 44, 44))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>                        

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try{
            db = new JFileChooser();
            WindowsFileChooserUI wui = new WindowsFileChooserUI(db);
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Grafico.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(Grafico.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Grafico.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedLookAndFeelException ex) {
                Logger.getLogger(Grafico.class.getName()).log(Level.SEVERE, null, ex);
            }
            wui.installUI(db);
        }catch(NullPointerException e)
        {}
        db.setAcceptAllFileFilterUsed(false);
        FileFilter ft = new FileNameExtensionFilter("Text Files (.txt)","txt");
        db.addChoosableFileFilter(ft);
        
        
        db.setFileSelectionMode(javax.swing.JFileChooser.FILES_AND_DIRECTORIES);
        
        int returnVal = db.showOpenDialog(this);
        
        
        java.io.File file = null;
        if(returnVal == javax.swing.JFileChooser.APPROVE_OPTION)
        {
            file = db.getSelectedFile();
        }else
        {
            file = db.getCurrentDirectory();
        }
        String fileName = file.toString();
        jLabel3.setText(file.getName());
        // TODO add your handling code here:
    }                                        

    private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                              
        if(jRadioButton3.isSelected())
        {
            jComboBox1.setVisible(true);
        }else{
            jComboBox1.setVisible(false);
        }
        jRadioButton2.setSelected(false);
        jRadioButton1.setSelected(false);
        // TODO add your handling code here:
    }                                             

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                              
        jComboBox1.setVisible(false);
        jRadioButton3.setSelected(false);
        jRadioButton1.setSelected(false);
        // TODO add your handling code here:
    }                                             

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
        jComboBox1.setVisible(false);
        jRadioButton2.setSelected(false);
        jRadioButton3.setSelected(false);
        // TODO add your handling code here:
    }                                             

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
		float var = 0;
		String tipo = "";
        String lista = "";
        String titulo = "";
        if(jLabel3.getText().contains(".txt") && (jRadioButton1.isSelected() || jRadioButton2.isSelected() || jRadioButton3.isSelected()))
        {
			if(jRadioButton1.isSelected())
            {
                titulo = "ArrayList";
                tipo = "AL";
            }else if(jRadioButton2.isSelected())
            {
                titulo = "Vector";
                tipo = "V";
            }else if(jRadioButton3.isSelected())
            {
                tipo = "L";
                switch(jComboBox1.getSelectedIndex()){
					case 0:
                        titulo = "Lista Simplemente encadenada";
						lista = "S";
						break;
					case 1:
                        titulo = "Lista Doblemente encadenada";
						lista = "D";
						break;
					case 2:
                        titulo = "Lista Circular";
						lista = "C";
						break;
						}
            }
			
			StackFactory<Float> sFactory = new StackFactory<Float>();
            Stack<Float> miStack = sFactory.getStack(tipo, lista);

            String path = db.getSelectedFile().getAbsolutePath();
            File archivo = new File(path);

            Archivo loco = new Archivo();
            var = loco.calcular(miStack, archivo);

			JOptionPane.showMessageDialog(t,"El resultado de la operacion es: "+var, titulo, JOptionPane.INFORMATION_MESSAGE);
        }else{
			if(!jLabel3.getText().contains(".txt"))
			{
				JOptionPane.showMessageDialog(t,"Debe seleccionar un archivo de texto, con extencion \".txt\"","Error",JOptionPane.ERROR_MESSAGE);
			}else{
				JOptionPane.showMessageDialog(t,"Debe seleccionar una de las opciones para realizar las operaciones.","Error",JOptionPane.ERROR_MESSAGE);
			}
        }
    }                                        


    // Variables declaration - do not modify                     
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JFileChooser db;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    // End of variables declaration                   
}
